﻿using System;

namespace gixoom.Dom.Score
{
    internal class Sha512StringValidator: ISha512StringValidator
    {
        private const int hexDigitsCount = 128;
        private char[] hexSymbols = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
            'A', 'B', 'C', 'D', 'E', 'F', 'a', 'b', 'c', 'd', 'e', 'f' };

        public Sha512StringValidator()
        {
            Array.Sort(hexSymbols);
        }

        /// <summary>
        /// Returns true if specified SHA512 string is valid.
        /// </summary>
        /// <param name="sha512String">SHA512 string</param>
        /// <returns>true if specified SHA512 string is valid.</returns>
        /// <exception cref="System.ArgumentNullException"><i>sha512String</i> is a null reference.</exception>
        public bool IsValid(string sha512String)
        {
            Ensure.ArgumentNotNull(sha512String, "sha512String");

            bool result = (sha512String.Length == hexDigitsCount);

            if (result)
            {
                foreach (char c in sha512String)
                {
                    int index = Array.BinarySearch(hexSymbols, c);

                    if (index < 0)
                    {
                        result = false;
                        break;
                    }
                }
            }

            return result;
        }
    }
}
